import React, { useState } from 'react';
import axios from 'axios';
import { Input, Button, Form, Space ,message} from 'antd';
const StudentCourseAssignment = () => {
  const [form] = Form.useForm();

  const handleAssignment = (values) => {
    console.log(`Assigning ${values.course} to ${values.grade} ${values.class} ${values.major}`);
    axios.post('http://localhost:8000/studentAssignment', { courseid: values.course,grade: values.grade,className: values.class,major: values.major })
      .then(response => {
        console.log(response.data); 
        message.success("添加成功")
      })
      .catch(error => {
        console.error('Error:', error.response.data.message);
        const errorMessage = error.response.data.message;
        message.error(errorMessage);
      });
  };

  return (
    <div>
      <Form form={form} onFinish={handleAssignment}>
        <Space direction="vertical">
          <Form.Item label="课程编号" name="course">
            <Input />
          </Form.Item>
          <Form.Item label="年级" name="grade">
            <Input />
          </Form.Item>
          <Form.Item label="班级" name="class">
            <Input />
          </Form.Item>
          <Form.Item label="专业" name="major">
            <Input />
          </Form.Item>

          <Form.Item>
            <Button type="primary" htmlType="submit">
              确认分配
            </Button>
          </Form.Item>
        </Space>
      </Form>
    </div>
  );
};

export default StudentCourseAssignment;
